<?php
/**
 * Created by 1971599474@qq.com
 * User: zhixuan
 * Date: 2020/1/28
 * Time: 23:21
 */

namespace app\index\controller;
use app\common\model\Order as OrderModel;

class Order extends MemberBase
{
    public function order_detail(){

    }


    public function index(){
        $params = array_filter(request()->param());
        $page = ! empty($params['page']) && $params['page'] >= 1 ? $params['page'] : 1;
        $where[] = ['a.deleted','=',0];
        if($order_sn = isset($params['order_sn'])){
            $where[] = ['a.order_sn','like',"%{$params['order_sn']}%"];
        }
        if($cat_id = isset($params['goods_name'])){
            $where[] = ['b.goods_name','like',"%{$params['goods_name']}%"];
        }

        $tmp = OrderModel::alias('a')
                                    ->field('a.shop_id,a.order_sn,a.order_id,a.pay_status,a.shipping_status,a.is_evaluate,a.deleted,a.add_time,a.order_amount,a.shipping_price,b.*')
                                    ->join('order_goods b','a.order_id=b.order_id')
                                    ->where($where)
                                    ->paginate(['query'=>request()->param()],10);
        $order = [];
        foreach($tmp as $k=>$v){
            $goods[$v['order_id']][] = [
                'goods_id'=>$v['goods_id'],
                'goods_name'=>$v['goods_name'],
                'goods_num'=>$v['goods_num'],
                'goods_price'=>$v['goods_price'],
                'discount_goods_price'=>$v['discount_goods_price'],
                'market_price'=>$v['market_price'],
                'spec_key_name'=>$v['spec_key_name'],
                'original_img'=>$v['original_img'],
                'is_send'=>$v['is_send'],
            ];
            $order[$v['order_id']] = [
                'order_sn'=>$v['order_sn'],
                'shop_name'=>$v['getShop']['shop_name'],
                'shop_id'=>$v['shop_id'],
                'order_id'=>$v['order_id'],
                'pay_status'=>$v['pay_status'],
                'shipping_status'=>$v['shipping_status'],
                'is_evaluate'=>$v['is_evaluate'],
                'a.deleted'=>$v['a.deleted'],
                'add_time'=>$v['add_time'],
                'order_amount'=>$v['order_amount'],
                'total_amount'=>$v['total_amount'],
                'shipping_price'=>$v['shipping_price'],
                'goodsList'=>$goods[$v['order_id']],
            ];
        }
        $dataProvider['order'] = $order;
        $dataProvider['page'] = $tmp->render();
        $this->assign('dataProvider',$dataProvider);
        return $this->fetch();
    }
}